package com.chunuo.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chunuo.commonutils.ordervo.CourseWebVoOrder;
import com.chunuo.commonutils.ordervo.UcenterMemberOrder;
import com.chunuo.order.entity.Order;
import com.chunuo.order.feign.CourseFrontOpenFeign;
import com.chunuo.order.feign.UcenterMemberOpenFeign;
import com.chunuo.order.mapper.OrderMapper;
import com.chunuo.order.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chunuo.order.utils.OrderNoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-28
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private CourseFrontOpenFeign courseFrontOpenFeign;

    @Autowired
    private UcenterMemberOpenFeign ucenterMemberOpenFeign;

    @Override
    public String createOrder(String courseId, String userId) {
        UcenterMemberOrder userInfo = ucenterMemberOpenFeign.getInfo(userId);
        CourseWebVoOrder courseInfo = courseFrontOpenFeign.getCourseInfo(courseId);


        //创建Order对象，向order对象里面设置需要数据
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//订单号
        order.setCourseId(courseId); //课程id
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName(courseInfo.getTeacherName());
        order.setTotalFee(courseInfo.getPrice());

        order.setMemberId(userId);
        order.setMobile(userInfo.getMobile());
        order.setNickname(userInfo.getNickname());
        order.setStatus(0);  //订单状态（0：未支付 1：已支付）
        order.setPayType(1);  //支付类型 ，微信1
        baseMapper.insert(order);
        //返回订单号
        return order.getOrderNo();
    }

    @Override
    public boolean getIsBuy(String courseId, String userId) {
        System.out.println("==========="+courseId+":"+userId);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("course_id",courseId);
        wrapper.eq("member_id",userId);
        wrapper.eq("status",1);
        Integer count = this.baseMapper.selectCount(wrapper);
        System.out.println("count == "+count);
        return count==0?false:true;
    }



}
